<%@ Page Language="C#" %>
<fb:FacebookApplication ID="fbApplication" runat="server" Mode="Fbml"
  ApplicationName="helloWorld"
  EnableDebugging="true" />

<fb:fbml version="1.0">

  <fb:header decoration="add_border">Hello World</fb:header>

  <div style="background-color: #eee; padding: 10px; border-bottom: solid 1px #ccc">
    <span style="font-weight: bold">
      Hello <fb:name uid="<%= fbApplication.Service.UserID %>" firstnameonly="true" useyou="false" ifcantsee="there" />!
    </span>
  </div>

  <div>
    <fb:editor action="Default.aspx" width="400" labelwidth="200">
      <fb:editor-custom labelwidth="200">
        <label><b>How about saying Hello to your friends?</b></label>
      </fb:editor-custom>
      <fb:editor-divider />
      <fb:editor-custom label="Choose your style">
        <select name="greeting">
          <option value="Hi!" selected>Hi!</option>
          <option value="Hey!">Hey!</option>
          <option value="Hello!">Hello!</option>
        </select>
      </fb:editor-custom>
      <fb:editor-custom label="Select some friends">
        <fb:multi-friend-input />
      </fb:editor-custom>
      <fb:editor-custom>
        <input type="checkbox" name="createStory" value="1" style="width: auto" />
        <label>Create mini-feed story</label><br />
        <span style="font-size: smaller; color: #aaa">You can create up to 10 stories every 2 days.</span>
      </fb:editor-custom>
      <fb:editor-divider />
      <fb:editor-buttonset>
        <fb:editor-button value="Send Greeting" />
        <fb:editor-cancel value="Cancel" href="<%= fbApplication.Service.ProfileUrl %>" />
      </fb:editor-buttonset>
    </fb:editor>
  </div>

</fb:fbml>

<script runat="server">
  
    private ArrayList GetSelectedUserIDs() {
        ArrayList ids = new ArrayList();
        int index = 0;
        while (true) {
            string id = Request.Form["ids[" + index + "]"];
            if (String.IsNullOrEmpty(id)) {
                break;
            }

            ids.Add(id);
            index++;
        }

        return ids;
    }
  
    private string BuildProfileFbml(ArrayList ids, string greeting) {
        StringBuilder fbmlBuilder = new StringBuilder();

        fbmlBuilder.Append("<div>");
        fbmlBuilder.Append("<fb:name uid=\"");
        fbmlBuilder.Append(fbApplication.Service.UserID);
        fbmlBuilder.Append("\" firstnameonly=\"true\" />");
        fbmlBuilder.Append(" said <b>");
        fbmlBuilder.Append(greeting);
        fbmlBuilder.Append("</b> to:");
        fbmlBuilder.Append("<fb:user-table>");

        for (int i = 0; i < ids.Count; i++) {
            fbmlBuilder.Append("<fb:user-item uid=\"");
            fbmlBuilder.Append((string)ids[i]);
            fbmlBuilder.Append("\" />");
        }

        fbmlBuilder.Append("</fb:user-table>");
        fbmlBuilder.Append("</div>");
        fbmlBuilder.Append("<fb:if-is-own-profile>");
        fbmlBuilder.Append("<div>");
        fbmlBuilder.Append("<a href=\"http://apps.facebook.com/nikhilhelloworld/Default.aspx\">Update your greeting</a>");
        fbmlBuilder.Append("</div>");
        fbmlBuilder.Append("</fb:if-is-own-profile>");

        return fbmlBuilder.ToString();
    }

    private string BuildStoryFbml(ArrayList ids, string greeting) {
        StringBuilder fbmlBuilder = new StringBuilder();

        fbmlBuilder.Append("<b>");
        fbmlBuilder.Append(greeting.Substring(0, greeting.Length - 1));
        fbmlBuilder.Append("</b> ");

        for (int i = 0; i < ids.Count; i++) {
            if (i != 0) {
                fbmlBuilder.Append(", ");
            }
            if ((ids.Count > 1) && (i == ids.Count - 1)) {
                fbmlBuilder.Append(" and ");
            }
            fbmlBuilder.Append("<fb:userlink uid=\"");
            fbmlBuilder.Append((string)ids[i]);
            fbmlBuilder.Append("\" />");
        }

        return fbmlBuilder.ToString();
    }

  protected override void OnLoad(EventArgs e) {
      base.OnLoad(e);

      ArrayList ids = GetSelectedUserIDs();
      if (ids.Count != 0) {
          FacebookService service = fbApplication.Service;
          
          string greeting = Request.Form["greeting"];
          string profileFbml = BuildProfileFbml(ids, greeting);

          service.Profile.SetFbml(profileFbml, null);

          if (!String.IsNullOrEmpty(Request.Form["createStory"])) {
              string titleFbml = "<fb:userlink uid=\"" + fbApplication.Service.UserID + "\" /> said '" + greeting + "'";
              string bodyFbml = BuildStoryFbml(ids, greeting);

              service.Feed.PublishMiniFeedStory(titleFbml, bodyFbml, null);
          }

          fbApplication.Redirect(this, fbApplication.Service.ProfileUrl);
      }
  }
</script>